﻿using CarterWechatServer.BizModel.Account;
using CarterWechatServer.DataEntity;
using CarterWechatServer.DataEntity.Entities;
using CarterWechatServer.Util.CustomException;
using Magicodes.WeChat.MiniProgram;
using Magicodes.WeChat.MiniProgram.Apis.Sns.Dto;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarterWechatServer.BizLogic.Account
{
    /// <summary>
    /// 账户管理
    /// </summary>
    public class AccountService
    {
        #region 账户管理==用户状态

        /// <summary>
        /// 获取注册状态
        /// </summary>
        /// <param name="code">登录接口code</param>
        /// <returns></returns>
        public UserStatusOutput UserStatus(string code)
        {
            //获取用户OpenId
            GetSnsInfoByCodeOutput output = MiniProgramApisContext.Current.SnsApi.GetSnsInfoByCode(code);

            if (!output.IsSuccess())
            {
                throw new AlertException("服务器连接失败");
            }

            using(EFContext db = new EFContext())
            {
                bool isRegister = db.UserExtraLogins.Any(m => m.OpenId == output.OpenId);
                return new UserStatusOutput()
                {
                    OpenId = output.OpenId,
                    IsRegister = isRegister
                };
            }

        }
        #endregion


        #region 账户管理==用户注册

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="input">用户注册输入</param>
        public void UserRegister(UserRegisterInput input)
        {
            using (EFContext db=new EFContext())
            {
                UserEntity userEntity = new UserEntity()
                {
                    NickName = input.NickName,
                    Gender = input.Gender,
                    HeadImageUrl = input.AvatarUrl,
                    UserExtraLogins = new List<UserExtraLoginEntity>()
                    {
                        new UserExtraLoginEntity()
                        {
                            OpenId = input.OpenId
                        }
                    }
                };

                db.Users.Add(userEntity);
                db.SaveChanges();
            }
        }
        #endregion

        #region 账户管理==用户详情

        /// <summary>
        /// 用户详情
        /// </summary>
        /// <param name="openId">Openid</param>
        /// <returns></returns>
        public UserInfoOutput UserInfo(string openId)
        {
            using(EFContext db = new EFContext())
            {
                UserInfoOutput output = db.UserExtraLogins
                    .Include(m => m.User)
                    .Where(m => m.OpenId == openId)
                    .Select(m => new UserInfoOutput()
                    {
                        UserId = m.UserId,
                        NickName = m.User.NickName
                    })
                    .FirstOrDefault();
                return output;
                                
            }
        }

        #endregion
    }
}
